import 'package:FiduMart/app_routes.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:FiduMart/constants/constants.dart';
import 'package:FiduMart/core/utils/image_constant.dart';
import 'package:FiduMart/core/utils/logger.dart';
import 'package:FiduMart/screen/home/QuotesController.dart';
import 'package:FiduMart/service/setting.dart';

import '../../localization/app_export.dart';

class Page1 extends StatefulWidget {
  @override
  State<Page1> createState() => _Page1State();
}

class _Page1State extends State<Page1> {

  final controller = Get.put(QuotesController());

  final List<String> _dropdownValues = langmap.keys.toList();

  String _selectedValue = "locale".tr;

  @override
  Widget build(BuildContext context) {
    return
        Container(
          decoration: BoxDecoration(
            gradient: LinearGradient(
              colors: [ColorConstant.whiteA700,ColorConstant.whiteA700],
              begin: FractionalOffset.centerLeft,
              end: FractionalOffset.centerRight,
            ),
          ),
          child: EasyRefresh(
            onRefresh: () async {
              // 下拉刷新逻辑
              controller.httpOnRefresh();
            },
            onLoad: () async{
              controller.httpOnLoad();
            },
            child: SingleChildScrollView(
              child: Column(
                children: [
                  SizedBox(height: 30,),
                  titleMainUI(),
                  Obx(() => sortUI()),
                  Obx(() => listMainUI()),
                ],
              ),
            ),
          ),
        );
  }

  Widget sortUI() {
    return Container(
      margin: EdgeInsets.only(left: 10,right: 10),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text('name_deal'.tr,
              style: TextStyle(fontSize: 14,color: ColorConstant.colorFF23262F)),
          GestureDetector(
            onTap: (){
              controller.currencyranklistSort();
            },
            child: Row(
              children: [
                Text('latest_price'.tr,
                    style: TextStyle(fontSize: 14,color: ColorConstant.colorFF23262F)),
                SizedBox(width: 3),
                /*       CustomImageView(
                imagePath: ImageConstant.icon_rise_down,
                width: 8,
                height: 14,
              ),*/
                SizedBox(width: 10),
                Text('24h_rise_fall'.tr,
                    style: TextStyle(fontSize: 14,color: ColorConstant.colorFF23262F)),
                SizedBox(width: 3),
                CustomImageView(
                  imagePath: controller.exchangeBuyStateTP(),
                  width: 8,
                  height: 14,
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget listMainUI() {
    return Flex(
      direction: Axis.vertical,
      children: [
        controller.investList.length == 0 ? Container(
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height-100,
            child:Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  CustomImageView(
                    imagePath: ImageConstant.noDate,
                    width: 134,
                    height: 134,
                  ),

                  Text('no_relevant_data_yet'.tr,
                    style: TextStyle(
                      color: ColorConstant.colorFF000000, // 文字颜色
                      fontSize: 12, // 文字大小
                    ),
                  ),
                ],
              ),
            )):
        buildList()
      ],
    );





  }

  Widget titleMainUI() {
    return Container(
      margin: EdgeInsets.only(left: 10),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          // Text('home_list'.tr,style: TextStyle(fontSize: 16,color: ColorConstant.colorFF23262F),),
          Expanded(child:
            GestureDetector(
              onTap: (){
                Get.toNamed(AppRoutes.currencySearch);
              },
              child: Container(
                padding: EdgeInsets.only(left: 10,right: 10),
                height: 44,
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                    colors: [ColorConstant.whiteA700,ColorConstant.whiteA700],
                    begin: FractionalOffset.centerLeft,
                    end: FractionalOffset.centerRight,
                  ),
                  borderRadius: BorderRadius.circular(4),
                  border:  Border.all(color: ColorConstant.colorFFCCCCCC, width: 1),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    CustomImageView(
                      imagePath: "assets/images/icon_mark_search.png",
                      width: 20,
                      height: 20,
                    ),
                    SizedBox(width: 5,),
                    Text("search_currency".tr,style: TextStyle(fontSize: 13,color: ColorConstant.colorFFB1B5C3)),
                  ],
                ),
              ),
            )
          ),
          TextButton(onPressed: (){

          }, child: DropdownButton<String>(
            value:  _selectedValue,
            icon: Image(image: AssetImage("assets/images/changeLangchooseupBtnImg.png"),width: 20,),
            underline: Container(),
            style: TextStyle(fontSize: 14),
            onChanged: (String? newValue) {
              setState(() {
                _selectedValue = newValue!;
                Setting.updateLang(_selectedValue);
              });
            },
            items: _dropdownValues.map((String value) {
              return DropdownMenuItem<String>(
                value: value,
                child: Text(langmap[value]!,style: TextStyle(color: ColorConstant.colorFF707280),),
              );
            }).toList(),
          ),)
        ],
      ),
    )    ;
  }

  ListView buildList() {
    return ListView.builder(
      padding: EdgeInsets.zero,
      itemCount: controller.investList.length,
      physics: NeverScrollableScrollPhysics(),
      shrinkWrap: true,
      itemBuilder: (BuildContext context, int index) {
        return GestureDetector(
          onTap: (){
            Logger.log('data-GestureDetector-jjjj');
            controller.getcurrencyKline(currencyrankItem:controller.investList[index],
                period:'15m');
          },
          child: Container(
            margin: EdgeInsets.only(top: 15,left: 10,right: 10),
            padding: EdgeInsets.only(top: 5,bottom: 5,right: 8),
            decoration: BoxDecoration(
              gradient: LinearGradient(
                colors: [ColorConstant.colorFFF2F2F7,ColorConstant.colorFFF2F2F7],
                begin: FractionalOffset.centerLeft,
                end: FractionalOffset.centerRight,
              ),
              borderRadius: BorderRadius.circular(16),
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Row(
                  children: [
                    CustomImageView(
                      url: controller.investList[index].logo.toString(),
                      fit: BoxFit.fill,
                      width: 48,
                      height: 48,
                      margin:EdgeInsets.all(10) ,
                      radius: BorderRadius.circular(24),
                    ),
                    Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(controller.investList[index].name.toString(),
                            style: TextStyle(
                              color: ColorConstant.colorFF000000, // 文字颜色
                              fontSize: 16, // 文字大小
                            )),
                        Text("\$".toString()+controller.toDoubleJudgingyiUstd(controller.investList[index].marketCapUsd.toString())+'price_yi'.tr,
                            style: TextStyle(
                              color: ColorConstant.colorFF828A99, // 文字颜色
                              fontSize: 12, // 文字大小
                            )),
                      ],
                    )
                  ],
                ),
                Row(
                  children: [
                    Column(
                      mainAxisAlignment: MainAxisAlignment.end,
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        Row(
                          children: [
                            Text(controller.titleDoubleJudgingGSH(controller.investList[index].priceUsd.toString()),
                                style: TextStyle(
                                  color: ColorConstant.colorFF000000, // 文字颜色
                                  fontSize: 14, // 文字大小
                                )),
                            SizedBox(width: 3),
                            CustomImageView(
                              imagePath: ImageConstant.icon_mak_rate,
                              width: 16,
                              height: 14,
                            ),
                          ],
                        ),
                        Text(controller.investList[index].percentChange24h.toString()+"%",
                            style: TextStyle(
                              color: (controller.investList[index].percentChange24h??0)>0 ? ColorConstant.colorFF39CC77:ColorConstant.colorFFE02020, // 文字颜色
                              fontSize: 12, // 文字大小
                            )),
                      ],
                    )
                  ],
                )
              ],
            ),
          ),
        ) ;
      }
    );
  }


}

